#!usr/bin/python
# -*- coding:utf-8 -*-
import re


class ReDemo:
    mess = "I am not a clever boy,but I is well diligent!"

    def _test_match(self):
        """
        rd = ReDemo()
        rd._test_match()
        """
        match_mess = re.match(r"(\w+)\s", self.mess)
        if match_mess:
            print match_mess.group(0), match_mess.group(1)
            print match_mess.string
            print match_mess.groups()
            print match_mess.pos
            print match_mess.endpos
            print match_mess.start(0)
            print match_mess.end(0)
            print match_mess.span(0)
            print match_mess.expand(r"\1 \1")
        else:
            print "no match!"

    def _test_search(self):
        """
        rd = ReDemo()
        rd._test_search()
        """
        search_mess = re.search(r"(\w+)\s", self.mess)
        search_result = search_mess.group(0) + search_mess.group(1) if search_mess else "no search!"
        print search_result

    def _test_sub(self):
        """
        rd = ReDemo()
        rd._test_sub()
        """
        sub_mess = re.sub(r"\s+", "-", self.mess)
        print sub_mess

    def _test_split(self):
        """
        rd = ReDemo()
        rd._test_split()
        """
        print re.split(r"\s+", self.mess)

    def _test_findall(self):
        """
        rd = ReDemo()
        rd._test_findall()
        """
        print re.findall(r"\w+\s", self.mess)

    def _test_compile(self):
        """
        rd = ReDemo()
        rd._test_compile()
        """
        pattern = re.compile(r"\w+\s")
        print pattern.findall(self.mess)

    def _test_finditer(self):
        """
        rd = ReDemo()
        rd._test_finditer()
        """
        for i in re.finditer(r"\w+\s", self.mess):
            print i.group()

    def _test_subn(self):
        """
        rd = ReDemo()
        rd._test_subn()
        """
        pattern = re.compile(r"\w+\s")
        print pattern.subn("hi-", self.mess)


if __name__ == '__main__':
    rd = ReDemo()
    rd._test_subn()
